package alp.starcode.framework.config;


import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * @author 芊宝宝
 * @create 2020/12/28
 */
@Configuration
@MapperScan(basePackages = {"alp.starcode.**"},sqlSessionFactoryRef =ChatDataSourceConfig.MYSQL_CHAT_SESSIONFACTORY )
public class ChatDataSourceConfig {
    public static final String MYSQL_CHAT_SESSIONFACTORY="chatSqlSessionFactory";
    public static final String MYSQL_CHAT_DATASOURCE="chatDataSource";
    public static final String MYSQL_CHAT_TRANSACTION="chatTransaction";
    public static final String MYSQL_CHAT_JDBCTEMPLATE="chatTemplate";

  //  private static final String CHAT_MAPPER_LOCAL = "classpath*:mapper/*.xml";


    @Bean(name = MYSQL_CHAT_DATASOURCE,initMethod = "init",destroyMethod = "close")
    @ConfigurationProperties(prefix = "spring.datasource.chat")
    public DruidDataSource chatDataSource()throws Exception{
        return new DruidDataSource();
    }

    @Bean(MYSQL_CHAT_JDBCTEMPLATE)
    public JdbcTemplate chatJdbcTemplate(@Qualifier(MYSQL_CHAT_DATASOURCE)DataSource dataSource)throws Exception{
        return new JdbcTemplate(dataSource);
    }

    @Bean(MYSQL_CHAT_SESSIONFACTORY)
    public SqlSessionFactory chatSqlSessionFactory(@Qualifier(MYSQL_CHAT_DATASOURCE)DataSource dataSource)throws Exception{
        MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean=new MybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactoryBean.setDataSource(dataSource);
      //`  mybatisSqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(CHAT_MAPPER_LOCAL));

        mybatisSqlSessionFactoryBean.setPlugins(new Interceptor[]{chatDataSourcePagingInterceptor()});
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setCacheEnabled(false);
        configuration.setDefaultFetchSize(512);
        // 配置打印sql语句
        configuration.setLogImpl(StdOutImpl.class);
        mybatisSqlSessionFactoryBean.setConfiguration(configuration);
        return mybatisSqlSessionFactoryBean.getObject();
    }

    @Bean(MYSQL_CHAT_TRANSACTION)
    public PlatformTransactionManager chatTransaction(@Qualifier(MYSQL_CHAT_DATASOURCE)DataSource dataSource)throws Exception{
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public PaginationInterceptor chatDataSourcePagingInterceptor(){
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作， true调回到首页，false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量，默认 500 条，-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }
}
